﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.DAL;
using SeDuce.BLL;

public partial class Management_Common_Type : PagerBase
{
    Lazy_Yu<TypeBLL> lazyTypeBLL = new Lazy_Yu<TypeBLL>(() => new TypeBLL());
    public TypeBLL LazyTypeBLL
    {
        get { return this.lazyTypeBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RegistrPager(this.GridView1, this.rptPageFliper, 7, this.FillPageMoudle(5000, SeDuce.DAL.TypeDAL.sqlGetTypes, new List<SortField>() { 
            new SortField() { FieldName = "Sort", DESC = false }
        }));
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "MOD")
        {
            try
            {
                GridViewRow row = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
                string strID = ((Label)this.GridView1.Rows[row.RowIndex].Cells[1].FindControl("lblID")).Text.Trim();
                string strName = ((Label)this.GridView1.Rows[row.RowIndex].Cells[2].FindControl("lblName")).Text.Trim();

                this.lblID.Text = strID;
                this.txtName.Text = strName;
            }
            catch { }
        }
        else if (e.CommandName == "DEL")
        {
            GridViewRow row = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
            string strID = ((Label)this.GridView1.Rows[row.RowIndex].Cells[1].FindControl("lblID")).Text.Trim();
            this.LazyTypeBLL.DeleteType(strID.ToInt());
            this.BindPage(this.rptPageFliper.UniqueID, 1);
        }
    }
    private void ResetForm()
    {
        this.lblID.Text = string.Empty;
        this.txtName.Text = string.Empty;
    }
    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.lblID.Text.Trim()))
        {
            //ADD
            if (this.LazyTypeBLL.InsertType(new SeDuce.Model.Type() { TypeName = this.txtName.Text.Trim(), Sort=0 }))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.reload();", "操作成功!"), true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
            }
        }
        else
        {
            //EDIT
            if (this.LazyTypeBLL.UpdateType(new SeDuce.Model.Type() { TypeID = this.lblID.Text.Trim().ToInt(), TypeName = this.txtName.Text.Trim(), Sort = 0 }))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key3", "javascript:" + string.Format("alert('{0}');window.location.reload();", "操作成功!"), true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key4", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
            }
        }
    }
    protected void lbtnCancel_Click(object sender, EventArgs e)
    {
        this.ResetForm();
    }
}